package com.fast.alden.admin.service.impl;

import com.fast.alden.admin.service.SysMessageService;
import com.fast.alden.admin.util.Constants;
import com.fast.alden.common.service.impl.BaseTreeNodeEntityServiceImpl;
import com.fast.alden.data.model.SysMessage;
import com.fast.alden.data.repo.SysMessageRepository;
import jakarta.annotation.Resource;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMessageServiceImpl extends BaseTreeNodeEntityServiceImpl<SysMessage, SysMessageRepository> implements SysMessageService {
    @Resource
    private SysMessageRepository repository;
    @Resource
    private CacheManager cacheManager;

    @Override
    protected Class<SysMessage> getEClass() {
        return SysMessage.class;
    }

    @Override
    protected SysMessageRepository getRepository() {
        return repository;
    }

    @Cacheable(cacheNames = Constants.CACHE_NAME_UNREAD_MESSAGE_COUNT)
    @Override
    public int countByReceiverIdIsAndReadStateIsFalse(Long receiverId) {
        return getRepository().countByReceiverIdIsAndReadStateIsFalse(receiverId);
    }

    /**
     * todo: 使用编程式缓存管理更新部分缓存，而非直接清除所有缓存
     */
    @CacheEvict(cacheNames = Constants.CACHE_NAME_UNREAD_MESSAGE_COUNT, allEntries = true)
    @Override
    public List<SysMessage> updateAll(Iterable<SysMessage> entities) {
        return super.updateAll(entities);
    }

    /**
     * todo: 使用编程式缓存管理更新部分缓存，而非直接清除所有缓存
     */
    @CacheEvict(cacheNames = Constants.CACHE_NAME_UNREAD_MESSAGE_COUNT, allEntries = true)
    @Override
    public List<SysMessage> createAll(Iterable<SysMessage> entities) {
        return super.createAll(entities);
    }
}
